/**
 * 
 */
package wrapper;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.csvreader.CsvReader;

import common.*;

/**
 * @author Or Kaplan
 *
 * float field wrapper
 */
public class FieldFloat extends FieldBase {

	/**
	 * @param tsvFieldName
	 * @param tableFieldName
	 * @param isRequired
	 */
	public FieldFloat(String tsvFieldName, String tableFieldName, boolean isRequired) {
		super(tsvFieldName, tableFieldName, isRequired);
	}

	/* (non-Javadoc)
	 * @see wrapper.IFieldWrapper#Validate(java.lang.String)
	 */
	@Override
	public boolean Validate(String data) {
		return !(this.isRequired && data.isEmpty());
	}

	/* (non-Javadoc)
	 * @see wrapper.IFieldWrapper#insertToStatement(java.sql.PreparedStatement, int, com.csvreader.CsvReader)
	 */
	@Override
	public boolean insertToStatement(PreparedStatement statement, int position,
			CsvReader reader) throws IOException, SQLException {
		
		String value = reader.get(this.getTsvFieldName());
		return insertToStatement(statement, position, value);
	}
	
	/* (non-Javadoc)
	 * @see wrapper.IFieldWrapper#insertToStatement(java.sql.PreparedStatement, int, java.lang.String)
	 */
	@Override
	public boolean insertToStatement(PreparedStatement statement, int position, String value) throws SQLException {	
		if (Validate(value))
		{
			if (value.isEmpty())
			{
				statement.setNull(position, java.sql.Types.FLOAT);
			}
			else
			{
				statement.setFloat(position, Float.parseFloat(value));
			}
			return true;
		}
		Logger.GetLogger().Log("invalid value at position " + position + ", " + value);
		return false;
	}

}
